package no.sachmuld.methodcombination.examples.reflection.clossimplestyle.equalshashcode.v2;

import no.sachmuld.methodcombination.api.BehaviourChain;
import no.sachmuld.methodcombination.api.reflection.And;

import java.util.Arrays;
import java.util.List;

/**
 *
 */
public class Point2D {

    private BehaviourChain<PointBehaviour> chain = new BehaviourChain<>();

    private int x;
    private int y;

    public Point2D(final int x, final int y) {

        this.x = x;
        this.y = y;

        with(new PointBehaviour() {

            @Override
            public List<Integer> hashCodes() {
                return Arrays.asList(x, y);
            }

            @Override
            public boolean equals(Object obj) {
                if (Point2D.this == obj) return true;
                if (obj == null || Point2D.this.getClass() != obj.getClass()) return false;

                Point2D other = (Point2D) obj;

                if (x != other.x) return false;
                if (y != other.y) return false;

                return true;

            }

        });

    }

    /*
    @Override
    public final int hashCode() {
        List<Integer> hashCodes = new Simple.Append<Integer>(this, chain, PointBehaviour.class, "hashCodes")
                .setDirection(AbstractMethodCombination.Direction.TOP_DOWN)
                .execute();
        int hashCode = 0;
        for (int h : hashCodes) {
            hashCode = 31 * hashCode + h;
        }
        return hashCode;
    }
    */

    @Override
    public final int hashCode() {
        return new HashCode<PointBehaviour>().build(PointBehaviour.class, Point2D.class, "hashCode").execute(this, chain);
    }

    @Override
    public final boolean equals(Object obj) {
        return new And().build(PointBehaviour.class, Point2D.class, "equals", Object.class).execute(this, chain, obj);
    }

    final protected Point2D with(PointBehaviour... behaviours) {
        chain.addAll(behaviours);
        return this;
    }

}
